package com.clothing.customization.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 订单数据实体类，用于存储设计师的订单统计数据
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "tb_order_data")
public class OrderData {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 设计师ID
     */
    @Column(name = "designer_id", nullable = false)
    private Long designerId;

    /**
     * 统计日期，按天统计
     */
    @Column(name = "stat_date", nullable = false)
    private LocalDateTime statDate;

    /**
     * 新接订单数
     */
    @Column(name = "new_order_count")
    private Integer newOrderCount;

    /**
     * 进行中订单数
     */
    @Column(name = "in_progress_count")
    private Integer inProgressCount;

    /**
     * 已完成订单数
     */
    @Column(name = "completed_count")
    private Integer completedCount;

    /**
     * 已取消订单数
     */
    @Column(name = "cancelled_count")
    private Integer cancelledCount;

    /**
     * 延期订单数
     */
    @Column(name = "delayed_count")
    private Integer delayedCount;

    /**
     * 订单总收入
     */
    @Column(name = "total_income", precision = 10, scale = 2)
    private BigDecimal totalIncome;

    /**
     * 订单平均评分
     */
    @Column(name = "average_rating", precision = 3, scale = 1)
    private BigDecimal averageRating;

    /**
     * 订单完成率
     */
    @Column(name = "completion_rate", precision = 5, scale = 2)
    private BigDecimal completionRate;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "create_time", updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "update_time")
    private LocalDateTime updateTime;

    /**
     * 在保存前设置默认值
     */
    @PrePersist
    public void prePersist() {
        if (newOrderCount == null) {
            newOrderCount = 0;
        }
        if (inProgressCount == null) {
            inProgressCount = 0;
        }
        if (completedCount == null) {
            completedCount = 0;
        }
        if (cancelledCount == null) {
            cancelledCount = 0;
        }
        if (delayedCount == null) {
            delayedCount = 0;
        }
        if (totalIncome == null) {
            totalIncome = BigDecimal.ZERO;
        }
    }
} 